﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace BioCyber_Infection.Class.Screens.Stages
{
    abstract class Stage
    {
        protected enum PacketType { STAGEDATA, PLAYERDATA };
        protected PacketType currentPackage;
        protected int updatesSinceStatusPacket = 0;
        protected const int updatesBetweenStatusPackets = 4;


        public bool LoadContent()
        {
            for (int i = 0; i < Network.networkSession.AllGamers.Count; i++)
            {
                if (Network.networkSession.AllGamers[i].IsLocal)
                {
                    updatesSinceStatusPacket = i;
                    break;
                }
            }
            return true;
        }

        public abstract void SendPackages();

        public abstract void ReceivePackages();




        protected virtual void SendPlayerData()
        {

            Engine.packetWriter.Write((int)PacketType.PLAYERDATA);
            foreach (LocalNetworkGamer networkGamer in Network.networkSession.LocalGamers)
            {
                //Associa o Tank com o jogador local
                Player localPlayer = networkGamer.Tag as Player;

                //Chamada do método UPDATE do Player Local.
                localPlayer.Update(networkGamer.SignedInGamer.PlayerIndex);


                /*if (updatesSinceStatusPacket >= updatesBetweenStatusPackets)
                {*/
                    updatesSinceStatusPacket = 0;
                    if ((Network.networkSession != null))
                    {
                        if (localPlayer != null)
                        {
                            Engine.packetWriter.Write((int)PacketType.PLAYERDATA);
                            Engine.packetWriter.Write(localPlayer.position);
                            Engine.packetWriter.Write(localPlayer.rotation);
                            networkGamer.SendData(Engine.packetWriter,
                                SendDataOptions.ReliableInOrder);
                        }
                    }
                    //networkGamer.SendData(Engine.packetWriter, SendDataOptions.ReliableInOrder);
                //}

                else
                {
                    updatesSinceStatusPacket++;
                }
            }
        }
        

        
        protected virtual void UpdatePlayerData(NetworkGamer sender)
        {
            if (sender != null)
            {
                Player remotePlayer = sender.Tag as Player;
                if (remotePlayer != null)
                {
                    remotePlayer.position = Engine.packetReader.ReadVector2();
                    remotePlayer.rotation = Engine.packetReader.ReadSingle();
                }
            }
                //remotePlayer.rotation = Engine.packetReader.ReadSingle();
        }
        

        
        protected virtual void SendEnemyData(LocalNetworkGamer host, Enemy[] enemyies)
        {
            currentPackage = PacketType.STAGEDATA;
            if (Network.networkSession.IsHost)
            {
                for (int i = 0; i < 1; i++)
                {
                    Engine.packetWriter.Write(enemyies[i].posicao);
                }
                host.SendData(Engine.packetWriter, SendDataOptions.ReliableInOrder);
            }
        }
        protected virtual void UpdateEnemyData(Enemy[] enemyies)
        {
            enemyies[0].posicao = Engine.packetReader.ReadVector2();
        }
        
    }
}
